<?php

namespace app\common\model;

use think\Model;
use think\model\concern\SoftDelete;

class cate extends Model
{
    use SoftDelete;
    protected $deleteTime = 'delete_time';

    //关联文章
    public function articleList(){
        return $this->hasMany('Article');
    }

    //栏目添加
    public function add($data){
        $validate = new \app\common\validate\Cate();
        if (!$validate->scene('add')->check($data)){
            return $validate->getError();
        }
        $result = $this->allowField(true)->save($data);
        if ($result){
            return 1;
        }else{
            return "栏目添加失败";
        }
    }

    //排序
    public function sort($data){
        $validate = new \app\common\validate\Cate();
        if (!$validate->scene("sort")->check($data)){
            return $validate->getError();
        }
        $sortData = $this->find($data['id']);
        $sortData['sort'] = $data['sort'];
        $result = $sortData->save();
        if ($result){
            return 1;
        }else{
            return "排序失败";
        }

    }

    //编辑
    public function edit($data){
        $validate = new \app\common\validate\Cate();
        if(!$validate->scene("add")->check($data)){
            return $validate->getError();
        }
        $cataInfo = $this->find($data['id']);
        $cataInfo->catename = $data['catename'];
        $cataInfo->sort = $data['sort'];
        $result = $cataInfo->save();
        if ($result){
            return 1;
        }else{
            return "修改失败";
        }
    }
}
